package org.atovio.http.config;

import org.atovio.http.service.TodoService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

/**
 * @author AtoVio, Winter Yuan
 */
@Configuration(proxyBeanMethods = false)
public class HttpConfiguration {

    // 创建服务接口的代理对象，基于WebClient
    @Bean
    public TodoService requestService() {
        WebClient webClient = WebClient.builder()
                .baseUrl("https://jsonplaceholder.typicode.com/").build();

        // 创建代理
        HttpServiceProxyFactory proxyFactory = HttpServiceProxyFactory.builder()
                .exchangeAdapter(WebClientAdapter.create(webClient)).build();

        // 创建某个接口的代理服务
        return proxyFactory.createClient(TodoService.class);
    }
}
